package com.hmall.controller.back;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.MallFreightMapper;
import com.hmall.mapper.MallShopMapper;

import com.hmall.model.MallFreight;

import com.hmall.vo.MallFreightDept;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/manage/logistics")
@Api(value = "后台物流管理操作API", description = "后台物流管理类API说明")
public class LogisticsManageController {
    private final
    MallFreightMapper mallFreightMapper;
    private final
    MallShopMapper mallShopMapper;

    @Autowired
    public LogisticsManageController(MallFreightMapper mallFreightMapper, MallShopMapper mallShopMapper) {
        this.mallFreightMapper = mallFreightMapper;
        this.mallShopMapper = mallShopMapper;
    }

    @AuthRequired
    @PostMapping("CreatedFreight")
    @ApiOperation(value = "添加物流价格")
    public ServerResponse<String> add(@RequestBody
                                              MallFreightDept mallFreightDept) {
        MallFreight mallFreight2 = mallFreightMapper.selectByArea(mallFreightDept.getArea());
        MallFreight mallFreight = new MallFreight();
        mallFreight.setArea(mallFreightDept.getArea());
        mallFreight.setEndPrice(mallFreightDept.getEndPrice());
        mallFreight.setRemarks(mallFreightDept.getRemarks());
        mallFreight.setStartPrice(mallFreightDept.getStartPrice());

        Integer shop_id = mallShopMapper.selectByDeptId(mallFreightDept.getDept_id());
        mallFreight.setShopId(shop_id);

        if (mallFreight2 == null) {
            mallFreightMapper.insert(mallFreight);

            return ServerResponse.createBySuccess("创建成功");
        }

        mallFreight.setFreightId(mallFreight2.getFreightId());
        mallFreightMapper.updateByPrimaryKey(mallFreight);

        return ServerResponse.createBySuccess("该物流价格已存在，修改成功");
    }

    @AuthRequired
    @GetMapping("GetFreightList")
    @ApiOperation(value = "获取物流价格列表")
    public ServerResponse<PageInfo<MallFreight>> GetFreightList(Integer dept_id,
                                                                @RequestParam(value = "pageNum", defaultValue = "1")
                                                                        int pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "100")
                                                                        int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<MallFreight> freightList = mallFreightMapper.selectByDeptId(dept_id);
        PageInfo<MallFreight> pageResult = new PageInfo<>(freightList);

        return ServerResponse.createBySuccess(pageResult);
    }

    @AuthRequired
    @DeleteMapping("DeleteFreight")
    @ApiOperation(value = "删除物流价格")
    public ServerResponse DeleteFreight(Integer[] id) {
        int product_id;
        int count = 0;

        for (Integer anId : id) {
            product_id = anId;
            count = count + mallFreightMapper.deleteByPrimaryKey(product_id);
        }

        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        } else {
            return ServerResponse.createBySuccess("删除成功");
        }
    }

    @AuthRequired
    @PostMapping("EditFreight")
    @ApiOperation(value = "修改物流价格")
    public ServerResponse EditFreight(
            @RequestBody
                    MallFreightDept mallFreightDept) {
        MallFreight mallFreight = new MallFreight();
        mallFreight.setArea(mallFreightDept.getArea());
        mallFreight.setEndPrice(mallFreightDept.getEndPrice());
        mallFreight.setRemarks(mallFreightDept.getRemarks());
        mallFreight.setStartPrice(mallFreightDept.getStartPrice());

        Integer shop_id = mallShopMapper.selectByDeptId(mallFreightDept.getDept_id());
        mallFreight.setShopId(shop_id);
        mallFreight.setFreightId(mallFreightDept.getFreightId());

        int count = mallFreightMapper.updateByPrimaryKey(mallFreight);

        if (count == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        } else {
            return ServerResponse.createBySuccess("修改成功");
        }
    }

    @AuthRequired
    @GetMapping("GetFreights")
    @ApiOperation(value = "获得物流价格信息")
    public ServerResponse<MallFreight> GetFreight(Integer id) {
        MallFreight freight = mallFreightMapper.selectByPrimaryKey(id);

        return ServerResponse.createBySuccess(freight);
    }

    @AuthRequired
    @GetMapping("SearchFreight")
    @ApiOperation(value = "搜索物流价格")
    public ServerResponse<PageInfo<MallFreight>> SearchFreight(String query,
                                                               @RequestParam(value = "pageNum", defaultValue = "1")
                                                                       int pageNum,
                                                               @RequestParam(value = "pageSize", defaultValue = "100")
                                                                       int pageSize) {
        System.out.println(query);
        PageHelper.startPage(pageNum, pageSize);

        List<MallFreight> freightList = mallFreightMapper.selectListByArea(query);
        PageInfo<MallFreight> pageResult = new PageInfo<>(freightList);

        return ServerResponse.createBySuccess(pageResult);
    }
}
